package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.Charge;
import com.czh.springboot04.service.ChargeService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/charge")
@CrossOrigin
public class ChargeController {
    @Autowired
    private ChargeService chargeService;

    @PostMapping("/insert")
    public R insert(@RequestBody Charge charge){
        int i = chargeService.insert(charge);
        if (i!=0){
            return R.ok("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @PutMapping("/update")
    public R update(@RequestBody Charge charge){
        int i = chargeService.update(charge);
        if (i!=0){
            return R.ok("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") int id){
        int i = chargeService.delete(id);
        if (i!=0){
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    @GetMapping("/selectByPage")
    public R selectByPage(int pageNum,int pageSize,Charge charge){
        //System.out.println(charge);
        PageInfo<Charge> pageInfo = chargeService.selectByPage(pageNum, pageSize,charge);
        return R.ok(pageInfo);
    }

    @GetMapping("/selectById")
    public R selectById(int id){
        Charge charge = chargeService.selectById(id);
        return R.ok(charge);
    }
    @GetMapping("/selectAll")
    public R selectAll(){
        List<Charge> charges = chargeService.selectAll();
        return R.ok(charges);
    }
}
